package com.mxpio.erp.plm.controller;

import com.mxpio.erp.common.plm.entity.ProjectInfoChangeRecord;
import com.mxpio.erp.common.plm.entity.ProjectInfoStatusHistory;
import com.mxpio.erp.common.plm.service.ProjectInfoChangeRecordService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "ProjectInfoChangeRecordController", description = "项目变更记录")
@RestController
@RequestMapping("/erp/plm/pro/info/change/record/")
public class ProjectInfoChangeRecordController {

    @Autowired
    private ProjectInfoChangeRecordService projectInfoChangeRecordService;

    @GetMapping("list")
    @Operation(summary = "项目变更记录列表", description = "项目变更记录列表（无分页）", method = "GET")
    public Result<List<ProjectInfoChangeRecord>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<ProjectInfoChangeRecord> list = projectInfoChangeRecordService.list(ProjectInfoChangeRecord.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "项目变更记录列表", description = "项目变更记录列表（分页）", method = "GET")
    public Result<Page<ProjectInfoChangeRecord>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<ProjectInfoChangeRecord> pageResult = projectInfoChangeRecordService.listPage(ProjectInfoChangeRecord.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{proCode}")
    @Operation(summary = "根据项目编码-获取项目变更记录", description = "根据项目编码-获取项目变更记录", method = "GET")
    public Result<List<ProjectInfoChangeRecord>> getById(@PathVariable(name = "proCode", required = true) String proCode) {
        Criteria criteria = Criteria.create().addCriterion("proCode", Operator.EQ, proCode);
        List<ProjectInfoChangeRecord> changeRecord = projectInfoChangeRecordService.list(ProjectInfoChangeRecord.class,criteria);
        return Result.OK(changeRecord);
    }

    @RequestMapping("add")
    @Operation(summary = "保存项目变更记录", description = "保存项目变更记录")
    public Result<ProjectInfoChangeRecord> add(@RequestBody ProjectInfoChangeRecord changeRecord) {
        projectInfoChangeRecordService.save(changeRecord);
        return Result.OK(changeRecord);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑项目变更记录", description = "编辑项目变更记录", method = "PUT")
    public Result<ProjectInfoChangeRecord> edit(@RequestBody ProjectInfoChangeRecord changeRecord) {
        projectInfoChangeRecordService.update(changeRecord);
        return Result.OK(changeRecord);
    }

    @DeleteMapping("delete")
    @Operation(summary = "通过id删除", description = "通过id删除", method = "DELETE")
    public Result<ProjectInfoStatusHistory> delete(@RequestParam(name="id",required=true) String id) {
        projectInfoChangeRecordService.delete(id,ProjectInfoChangeRecord.class);
        return Result.OK();
    }
}
